<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>查找替换网页编辑器</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 20px;
            display: flex;
            flex-direction: column;
            align-items: center;
        }

        textarea {
            width: 80%;
            height: 600px;
            margin-bottom: 20px;
            padding: 10px;
            font-size: 14px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
        }

        .find-replace-container {
            display: flex;
            justify-content: center;
            align-items: center;
            margin-bottom: 20px;
        }

        .find-replace-container input {
            margin: 0 10px;
            padding: 5px;
            font-size: 14px;
        }

        button {
            padding: 10px 20px;
            font-size: 14px;
            cursor: pointer;
            background-color: #007BFF;
            color: white;
            border: none;
            border-radius: 5px;
        }

        button:hover {
            background-color: #0056b3;
        }

        .button-container {
            display: flex;
            justify-content: center;
            gap: 10px;
        }
    </style>
</head>

<body>
    <textarea id="editor" placeholder="在此输入文本..."></textarea>
    <div class="find-replace-container">
        <input type="text" id="findText" placeholder="查找">
        <input type="text" id="replaceText" placeholder="替换为">
        <button onclick="findAndReplace()">查找并替换</button>
    </div>
    <div class="button-container">
        <button onclick="copyText()">一键复制文本内容</button>
    </div>
    <script>
        function findAndReplace() {
            const findText = document.getElementById('findText').value;
            const replaceText = document.getElementById('replaceText').value;
            const editor = document.getElementById('editor');

            if (findText === '') {
                alert('请输入要查找的文本。');
                return;
            }

            // 使用正则表达式进行全局替换
            const regex = new RegExp(findText, 'g');
            editor.value = editor.value.replace(regex, replaceText);
        }

        function copyText() {
            const editor = document.getElementById('editor');
            editor.select();
            editor.setSelectionRange(0, 99999); // For mobile devices

            try {
                document.execCommand('copy');
                // alert('文本已复制到剪贴板!');
            } catch (err) {
                console.error('复制失败:', err);
                alert('复制失败，请手动复制文本');
            }
        }
    </script>
</body>

</html>